package net.joint_painting.server.service;

import java.util.List;
import java.util.logging.Logger;

import net.joint_painting.server.manager.UpdatesManager;
import net.joint_painting.server.manager.WorkManager;
import net.joint_painting.server.model.MemCache;
import net.joint_painting.server.model.JointPaintingSurface;
import net.joint_painting.shared.model.Line;
import net.joint_painting.shared.model.Work;
import net.joint_painting.shared.service.Service;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * @author CAM
 * @version $Revision: 1.0 $
 */
public class ServiceImpl extends RemoteServiceServlet implements Service {

    /**
	 * 
	 */
    private static final long serialVersionUID = -2312176052405274928L;

    private static final Logger log = Logger.getLogger(ServiceImpl.class
	    .getName());

    /**
     * Method drawLines.
     * 
     * @param lines
     *            List<Line>
     * 
     * 
     * @throws IllegalArgumentException
     *             * @see
     *             net.joint_painting.shared.service.Service#drawLines(List
     *             <Line>)
     */
    @Override
    public void drawLines(List<Line> lines) throws IllegalArgumentException {
	for (Line line : lines)
	    WorkManager.addLine(line);
    }

    /**
     * Method drawSurface.
     * 
     * @param id
     *            String
     * @param data
     *            String
     * 
     * 
     * @throws IllegalArgumentException
     *             * @see net.joint_painting.shared.service.Service#drawSurface(
     *             String, String)
     */
    @Override
    public void drawSurface(String id, String data)
	    throws IllegalArgumentException {
	net.joint_painting.server.logic.Surface.drawSurface(id, data);
    }

    /**
     * Method getSurface.
     * 
     * @param id
     *            String
     * 
     * 
     * 
     * @return String * @throws IllegalArgumentException * @see
     *         net.joint_painting.shared.service.Service#getSurface(String)
     */
    @Override
    public String getSurface(String id) throws IllegalArgumentException {

	String data = (String) MemCache.getCached(id);
	if (data == null) {
	    JointPaintingSurface surface = JointPaintingSurface.getSurface(id);
	    if (surface != null)
		data = surface.getData();
	    if (data != null)
		MemCache.cache(id, data);
	}
	if (data == null)
	    data = "";
	return data;
    }

    /**
     * Method getUpdatedSurfaces.
     * 
     * 
     * 
     * @return List<String> * @throws IllegalArgumentException * @see
     *         net.joint_painting.shared.service.Service#getUpdatedSurfaces()
     */
    @Override
    public List<String> getUpdatedSurfaces() throws IllegalArgumentException {
	return UpdatesManager.getUpdates();
    }

    /**
     * Method getWork.
     * 
     * 
     * 
     * @return Work * @throws IllegalArgumentException * @see
     *         net.joint_painting.shared.service.Service#getWork()
     */
    @Override
    public Work getWork() throws IllegalArgumentException {
	return WorkManager.getWork();
    }

    /**
     * Method sendWork.
     * 
     * @param work
     *            Work
     * 
     * 
     * @throws IllegalArgumentException
     *             * @see
     *             net.joint_painting.shared.service.Service#sendWork(Work)
     */
    @Override
    public void sendWork(Work work) throws IllegalArgumentException {
	WorkManager.handWork(work);
    }

}
